package com.lz.ionicbase.view;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.lz.ionicbase.R;
import com.lz.ionicbase.utils.BaseUtil;
import com.lz.ionicbase.utils.Constant;
import com.lz.ionicbase.utils.SpUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 轮播图中的小黑点
 */

public class MyIndicator extends RelativeLayout {
    final List<View> viewList = new ArrayList<View>();

    public MyIndicator(Context context) {
        super(context);
    }

    public MyIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public void init(Context mContext,  final int position, final int size, ViewPager viewPager){
        FrameLayout.LayoutParams indicatorLayoutParams
                = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                BaseUtil.dip2px(mContext, 19));
        LinearLayout linearLayout = new LinearLayout(mContext);
        linearLayout.setGravity(Gravity.BOTTOM| Gravity.CENTER_HORIZONTAL);

        addView(linearLayout, indicatorLayoutParams);

        boolean isHandler = SpUtils.getBoolean(mContext, Constant.ISHANDLER, true);
            for (int i = 0; i < size; i++) {
                if (isHandler){
                View view = new View(mContext);
                    view.setBackgroundResource(i == position ? R.drawable.selected_point_shape : R.drawable.unselected_point_shape);
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(BaseUtil.dip2px(mContext, 6), BaseUtil.dip2px(mContext, 6));
                    layoutParams.setMargins(0, 0, BaseUtil.dip2px(mContext, 6), BaseUtil.dip2px(mContext, 6));
                    linearLayout.addView(view, layoutParams);
                    viewList.add(view);
                }
                //小于一张图片数轮播点不显示
                linearLayout.setVisibility(size <= 1 ? INVISIBLE : VISIBLE);
            }

//        if (size != 0) {
//            int index = position % size;
//            for (int i = 0; i < size; i++) {
//                View view = viewList.get(i);
//                view.setBackgroundResource(index == i? R.drawable.selected_point_shape : R.drawable.unselected_point_shape);
//            }
//        }
//        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            @Override
//            public void onPageSelected(int arg0) {
//                if (size != 0) {
//                    int index = arg0 % size;
//                    for (int i = 0; i < size; i++) {
//                        View view = viewList.get(i);
//                        view.setBackgroundResource(index == i? R.drawable.selected_point_shape : R.drawable.unselected_point_shape);
//                    }
//                }
//            }
//
//            @Override
//            public void onPageScrolled(int arg0, float arg1, int arg2) {
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int arg0) {
//
//            }
//        });
    }
    public List<View>  getIndicator(){
        return  viewList;
    }
}
